﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// DateOnly json转换器
    /// </summary>
    public class DateOnlyJsonValueConverter : JsonValueConverterAbs
    {
        private readonly static DateOnlyJsonValueConverter _default;
        /// <summary>
        /// 默认实例
        /// </summary>
        public static DateOnlyJsonValueConverter Default
        {
            get { return _default; }
        }

        static DateOnlyJsonValueConverter()
        {
            _default = new DateOnlyJsonValueConverter();
        }





        /// <summary>
        /// 构造函数
        /// </summary>
        public DateOnlyJsonValueConverter()
            : base(true)
        {

        }





        /// <summary>
        /// 尝试将对象值转换为json值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="value">属性或字段值</param>
        /// <param name="jsonValue">成员值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(MemberInfo memberInfo, Type valueType, object owner, object value, out string jsonValue)
        {
            bool result = true;

            if (value == null)
            {
                if (JsonHelper.AllowNull(valueType))
                {
                    //可空类型,值为null
                    jsonValue = null;
                }
                else
                {
                    //非可空类型,值为null,不符合预期
                    jsonValue = null;
                    result = false;
                }
            }
            else
            {
                jsonValue = $"{((DateOnly)value).ToString(JsonConstants.DATET_FORMAT)}";
            }

            return result;
        }





        /// <summary>
        /// 尝试将json值转换为对象值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="jsonValue">json值</param>
        /// <param name="value">对象值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertFrom(MemberInfo memberInfo, Type valueType, object owner, string jsonValue, out object value)
        {
            bool result = true;

            if (string.IsNullOrWhiteSpace(jsonValue) || JsonHelper.IsNull(jsonValue))
            {
                if (JsonHelper.AllowNull(valueType))
                {
                    //非可空类型,但是值为null,不符合预期
                    value = null;
                }
                else
                {
                    value = null;
                    result = false;
                }
            }
            else
            {
                DateOnly dto;
                result = DateOnly.TryParse(jsonValue, out dto);
                if (result)
                {
                    value = dto;
                }
                else
                {
                    //json值不是有效的DateOnly格式值
                    value = null;
                }
            }

            return result;
        }
    }
}
